<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系列名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入系列名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:series:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:series:edit']"
        >修改</el-button>
      </el-col>
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="seriesList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="code" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="时序表名" align="center" prop="tableName" >
        <template slot-scope="scope">
          {{scope.row.tableName }}
          <el-button
            v-if="!scope.row.stableCreate"
            size="mini"
            type="text"
            icon="el-icon-circle-plus-outline"
            @click="createSTable(scope.row)"
            v-hasPermi="['device:series:manager']"
          >创建表</el-button>
        </template>
      </el-table-column>
      <el-table-column label="协议类型" align="center" prop="defaultProtocol">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.communication_protocol" :value="scope.row.defaultProtocol"/>
        </template>
      </el-table-column>
      <el-table-column label="获取序列号方式" align="center" prop="serialModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_serial_model" :value="scope.row.serialModel"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="序列号属性" align="center" prop="serialAttr" /> -->
      <!-- <el-table-column label="数据对象属性" align="center" prop="dataAttr" /> -->
      <!-- <el-table-column label="串口标识属性" align="center" prop="serialFlagAttr" /> -->
      <!-- <el-table-column label="MQTT主题" align="center" prop="subscribeTopic" /> -->
      <el-table-column label="序列号截取规则" align="center" prop="subscriptSerialRules" />
      <el-table-column label="主题订阅操作" align="center" prop="subscriptSerialRules">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-plus-outline"
            @click="toSubscript(scope.row)"
            v-hasPermi="['device:series:edit']"
          >订阅</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-remove-outline"
            @click="toUnSubscript(scope.row)"
            v-hasPermi="['device:series:edit']"
          >取消订阅</el-button>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:series:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="toInteraction(scope.row)"
            v-hasPermi="['device:series:edit']"
          >数据交互</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="toStatusConfig(scope.row)"
            v-hasPermi="['device:series:edit']"
          >状态设置</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="toSeriesIo(scope.row)"
            v-hasPermi="['device:series:edit']"
          >串口定义</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="toMessage(scope.row)"
            v-hasPermi="['device:series:edit']"
          >报文定义</el-button>     
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="toAttribute(scope.row)"
            v-hasPermi="['device:series:edit']"
          >基础属性</el-button>    
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:series:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备系列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入编号" :disabled="form.stableCreate" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="时序表名" prop="tableName">
          <el-input v-model="form.tableName" placeholder="请输入名称" :disabled="form.stableCreate"/>
        </el-form-item>
        <el-form-item label="协议类型" prop="defaultProtocol">
          <el-select v-model="form.defaultProtocol" placeholder="请选协议类型">
            <el-option
              v-for="dict in dict.type.communication_protocol"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="获取序列号方式" prop="serialModel">
          <el-select v-model="form.serialModel" placeholder="请选择获取序列号方式">
            <el-option
              v-for="dict in dict.type.device_serial_model"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="序列号属性" prop="serialAttr">
          <el-input v-model="form.serialAttr" placeholder="请输入序列号属性" />
        </el-form-item> -->
        <!-- <el-form-item label="串口标识属性" prop="serialFlagAttr">
          <el-input v-model="form.serialFlagAttr" placeholder="请输入串口标识属性" />
        </el-form-item> -->
        <!-- <el-form-item label="MQTT主题" prop="subscribeTopic">
          <el-input v-model="form.subscribeTopic" placeholder="请输入MQTT主题" />
        </el-form-item> -->
        <el-form-item label="序列号截取规则" prop="subscriptSerialRules">
          <el-input v-model="form.subscriptSerialRules" placeholder="请输入序列号截取规则" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSeries, getSeries, delSeries, addSeries, updateSeries , subscript , unSubscript ,createSTable } from "@/api/device/series";

export default {
  name: "Series",
  dicts: ['device_serial_model', 'communication_protocol'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备系列表格数据
      seriesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {
        stableCreate:false
      },
      // 表单校验
      rules: {
        code: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        defaultProtocol: [
          { required: true, message: "协议类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备系列列表 */
    getList() {
      this.loading = true;
      listSeries(this.queryParams).then(response => {
        this.seriesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code:null,
        stableCreate:false,
        name: null,
        tableName:null,
        defaultProtocol: null,
        serialModel: null,
        serialAttr: null,
        dataAttr: null,
        deviceTimeAttr:null,
        dateTimeFormat:null,
        serialFlagAttr: null,
        subscribeTopic: null,
        subscriptSerialRules: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备系列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSeries(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备系列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSeries(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSeries(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备系列编号为"' + ids + '"的数据项？').then(function() {
        return delSeries(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //添加到订阅
    toSubscript(row){
      subscript(row.id).then( res => this.$modal.msgSuccess("操作成功！"))
    },
    //取消订阅
    toUnSubscript(row){
      unSubscript(row.id).then( res => this.$modal.msgSuccess("操作成功！"))
    },
    //创建超级表
    createSTable(row){
      createSTable(row.id).then( res => this.$modal.msgSuccess("操作成功！"))
    },
    //跳转到数据交互配置
    toInteraction(row){
      this.$router.push({
        path: '/sysDevice/interaction',
        query: { seriesId: row.id }
      })
    },
    //跳转到数据交互配置
    toStatusConfig(row){
      this.$router.push({
        path: '/sysDevice/statusConfig',
        query: { seriesId: row.id }
      })
    },
    //跳转到数据交互配置
    toSeriesIo(row){
      this.$router.push({
        path: '/sysDevice/seriesIo',
        query: { seriesId: row.id }
      })
    } ,
    
    //跳转到数据交互配置
    toMessage(row){
      this.$router.push({
        path: '/sysDevice/seriesMessage',
        query: { seriesId: row.id }
      })
    } ,
    //通用属性配置
    toAttribute(row){
      this.$router.push({
        path: '/sysDevice/seriesAttribute',
        query: { seriesId: row.id }
      })
    },
    //基础属性配置
    toDeviceType(row){
      this.$router.push({
        path: '/sysDevice/deviceType',
        query: { seriesId: row.id }
      })
    }
  }
};
</script>
